<template>
  <div class="user-wrapper" :class="theme">
    <span class="action">
      <!-- <a class="logout_title" target="_blank" href="http://jeecg-boot.mydoc.io">
        <a-icon type="question-circle-o"></a-icon>
      </a> -->
      <a-select size="small" style="width:100px;" :placeholder="$t('common.chooselang')" @change="chooseLang">
        <a-select-option value="zh-cn">中文</a-select-option>
        <a-select-option value="vi-vn">Vietnam</a-select-option>
        <a-select-option value="en-us">english</a-select-option>
      </a-select>
    </span>
    <!-- <header-notice class="action"/> -->
    <a-dropdown>
      <span class="action action-full ant-dropdown-link user-dropdown-menu">
        <a-avatar class="avatar" size="small" :src="getAvatar()"/>
        <span v-if="isDesktop()">{{ $t('common.welcome') }}，{{ nickname() }}</span>
      </span>
      <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
        <!-- <a-menu-item key="0">
          <router-link :to="{ name: 'account-center' }">
            <a-icon type="user"/>
            <span>个人中心</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="1">
          <router-link :to="{ name: 'account-settings-base' }">
            <a-icon type="setting"/>
            <span>账户设置</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="3"  @click="systemSetting">
           <a-icon type="tool"/>
           <span>系统设置</span>
        </a-menu-item>
        -->
         
        <a-menu-item key="4" @click="updatePassword">
          <a-icon type="setting"/>
          <span>{{$t('user.editPassBtn')}}</span>
        </a-menu-item>
        <!-- <a-menu-item key="5" @click="updateCurrentDepart">
          <a-icon type="cluster"/>
          <span>切换部门</span>
        </a-menu-item> -->
       <!-- <a-menu-item key="2" disabled>
          <a-icon type="setting"/>
          <span>测试</span>
        </a-menu-item>
        <a-menu-divider/>
        <a-menu-item key="3">
          <a href="javascript:;" @click="handleLogout">
            <a-icon type="logout"/>
            <span>退出登录</span>
          </a>
        </a-menu-item>-->
      </a-menu>
    </a-dropdown>
    <span class="action">
      <a class="logout_title" href="javascript:;" @click="handleLogout">
        <a-icon type="logout"/>
        <span v-if="isDesktop()">&nbsp;{{$t('login.loginouttxt')}}</span>
      </a>
    </span>
    <user-password ref="userPassword"></user-password>
    <depart-select ref="departSelect" :closable="true" title="部门切换"></depart-select>
    <setting-drawer ref="settingDrawer" :closable="true" title="系统设置"></setting-drawer>
  </div>
</template>

<script>
  import HeaderNotice from './HeaderNotice'
  import UserPassword from './UserPassword'
  import SettingDrawer from "@/components/setting/SettingDrawer";
  import DepartSelect from './DepartSelect'
  import { mapActions, mapGetters } from 'vuex'
  import { mixinDevice } from '@/utils/mixin.js'
  import moment from 'moment'
  import 'moment/locale/zh-cn'
  import Cookies from 'js-cookie'
  import store from '@/store/'

  moment.locale('zh-cn')
  const EN = 'en-us'
  const ZH = 'zh-cn'
  const langCookieKey = 'lang'
  export default {
    name: "UserMenu",
    mixins: [mixinDevice],
    components: {
      HeaderNotice,
      UserPassword,
      DepartSelect,
      SettingDrawer
    },
    props: {
      theme: {
        type: String,
        required: false,
        default: 'dark'
      }
    },
     data () {
      return {
        isZHCN:true
      }
    },
    created () {
      if (Cookies.get(langCookieKey)) {
          let lang = Cookies.get(langCookieKey)
          if (lang != null && lang === ZH) {
              this.isZHCN = true
              this.$i18n.locale = ZH
              moment.locale(ZH)
          } else {
             this.isZHCN = false
             this.$i18n.locale = EN
              moment.locale(EN)
          }
          
      }
    },
    methods: {
      moment,
      ...mapActions(["Logout"]),
      ...mapGetters(["nickname", "avatar","userInfo"]),
      getAvatar(){
        console.log('url = '+ window._CONFIG['imgDomainURL']+"/"+this.avatar())
        return window._CONFIG['imgDomainURL']+"/"+this.avatar()
      },
      chooseLang(lang) {
        this.$store.dispatch('setLang',  lang)
          this.$i18n.locale = lang
          moment.locale(ZH)
          Cookies.set(langCookieKey, lang)
           window.location.reload();
      },
      handleLogout() {
        const that = this
        let loginoutText = this.$t('login.loginoutWarn');
        let warnText = this.$t('common.warn');
        this.$confirm({
          title: warnText,
          content: loginoutText,
          onOk() {
            return that.Logout({}).then(() => {
                window.location.href="/";
              //window.location.reload()
            }).catch(err => {
              that.$message.error({
                title: '错误',
                description: err.message
              })
            })
          },
          onCancel() {
          },
        });
      },
      updatePassword(){
        let username = this.userInfo().username
        this.$refs.userPassword.show(username)
      },
      updateCurrentDepart(){
        this.$refs.departSelect.show()
      },
      systemSetting(){
        this.$refs.settingDrawer.showDrawer()
      }
    }
  }
</script>

<style scoped>
  .logout_title {
    color: inherit;
    text-decoration: none;
  }
</style>